﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace C_sports_meet
{
    /// <summary>
    /// registPage.xaml 的交互逻辑
    /// </summary>
    public partial class registPage : Window
    {
        public registPage()
        {
            InitializeComponent();
        }
        String strImage = "";
        public void Image_MouseDown(object sender, RoutedEventArgs e)
        {
            //创建OpenFileDialog对象
            OpenFileDialog openFileDialog = new OpenFileDialog();

            //创建一个筛选器
            openFileDialog.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp";

            //设置对话框标题
            openFileDialog.Title = "打开图片`:";


            //如果结果为打开，则选定文件
            if ((bool)openFileDialog.ShowDialog())
            {
                strImage = openFileDialog.FileName;
                BitmapImage curBitmap = new BitmapImage(new Uri(strImage));
                Image.Source = curBitmap;
            }



        }

        private void registButtonClick(object sender, RoutedEventArgs e)//注册按钮点击事件
        {
            string r_username = r_txtUserName.Text.Trim();
            string r_Pwd = r_RtxtPwd.Text.Trim();
            string r_Pwd1 = r_RtxtPwd1.Text.Trim();
            string r_mail = r_Email.Text.Trim();

            if (r_RtxtPwd.Text != r_RtxtPwd1.Text)
            {
                MessageBox.Show("两次输入密码不一致，请重新输入！");
                r_RtxtPwd.Text = "";
                r_RtxtPwd1.Text = "";
            }
            if (r_username == "" || r_Pwd == "" || r_Pwd1 == "" || r_mail == "")
            {
                MessageBox.Show("请完整填写信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
            }        

            else
            {
                SqlConnection sqlConnection = new SqlConnection("server=;database=sportmeet;user=sa;pwd=Lss200217");

                string add_data = "insert into dbo.Users values(@UserName,@PassWord,@Email,@Image)";
                SqlCommand cmd1 = new SqlCommand(add_data, sqlConnection);

                string sql2 = "select UserName from dbo.Users where UserName='" + r_username + "'";
                SqlCommand cmd2 = new SqlCommand(sql2, sqlConnection);

                SqlParameter sqlParameter = new SqlParameter("@UserName", r_username);
                cmd1.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@PassWord", r_Pwd);
                cmd1.Parameters.Add(sqlParameter);            
                sqlParameter = new SqlParameter("@Email", r_mail);
                cmd1.Parameters.Add(sqlParameter);

                //创建文件流对象
                FileStream fs = new FileStream(strImage, FileMode.Open, FileAccess.Read);
                //创建二进制流对象
                BinaryReader Breader = new BinaryReader(fs);
                //得到字节数组
                byte[] byteImage = Breader.ReadBytes((int)fs.Length);
                cmd1.Parameters.Add("@Image", System.Data.SqlDbType.Image).Value = byteImage;
                sqlConnection.Open();
                SqlDataReader sqlDataReader = cmd2.ExecuteReader();
                if (sqlDataReader.HasRows)
                {
                    MessageBox.Show("该账号已被注册！", "notice", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    return;
                }
                sqlDataReader.Close();
                SqlDataReader sqlDataReader1 = cmd1.ExecuteReader();

                sqlConnection.Close();
                r_txtUserName.Text = "";
                r_RtxtPwd.Text = "";
                r_RtxtPwd1.Text = "";
                MessageBox.Show("注册成功");
                Login login = new Login();
                this.Close();
                login.Show();

            }
        }

        private void backButtonClick(object sender, RoutedEventArgs e)
        {
            Login login = new Login();
            this.Close();
            login.Show();
        }
    }
}
